<?php

class FormAction_Delete extends FormAction
{

  protected $action = Form::ACTION_VIEW;

  public function __construct(Form $form)
  {
    parent::__construct($form);
  }

  public function execute()
  {
    $this->loadData();
    $this->delete();
  }

  public function render()
  {
    $view = $this->getView();
    $view->set('data', $this->getIterator());
    return parent::render();
  }

  protected function delete()
  {
    $ids = $this->form->getId();

    foreach ((array) $ids as $id) {
      $this->gateway()->delete($id);
    }
  }

  public function loadData()
  {
    foreach ((array) $this->form->getId() as $id) {
      $params = array();

      $params['fields'] = array($this->form->getPk());
      $params['where'] = array($this->form->getPk() . ' = :' . $this->form->getPk());
      $params['data'] = array($this->form->getPk() => $id);

      $row = $this->gateway()->find($params);

      $this->data[] = $row;
    }
  }

}
